package com.news.controller;

import com.news.common.ResponseBean;
import com.news.common.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 映射路径控制类
 * */
@Controller
public class IndexController {


//    @Autowired
//    SysSwiperService swiperService;

    //自定义错误状态拦截
    @RequestMapping("/error/{status}")
    public String errorPage(@PathVariable Integer status){
        switch (status){
            case 401:
            case 404:return "/error/404";
            case 500:return "/error/500";
            default:return "/error/default";
        }
    }
    /**
     * 跳到前台首页
     * @param session
     * @return
     */
    //新闻展示页面映射
    @RequestMapping({"/","home/index"})
    public String index(HttpSession session, Model model) {
//        List<SysSwiper> list = swiperService.list();
//        model.addAttribute("swiperList",list);
        return "home/index";
    }
    //跳到后台主页面
    @RequestMapping({"/user/index"})
    public String userIndex(HttpSession session) {
        if (session.getAttribute("user") != null) {
            return "index";
        }
        return "login";
    }

    //后台登录
    @RequestMapping({"/login","/admin/login"})
    public String login(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }
    //跳到前台登录页面
    @RequestMapping({"/home/login"})
    public String homeLogin(HttpSession session) {
        session.removeAttribute("user");
        return "home/user/login";
    }

    @ApiOperation("退出登录")
    @RequestMapping( value = {"/logout","/home/logOut","/logOut","admin/logOut"})
    @ResponseBody
    public ResponseBean logout(HttpSession session){
        session.removeAttribute("user");
        return ResultUtil.success();
    }
//前台注册
    @RequestMapping("/register")
    public String togologin(Model model){
        model.addAttribute("title","注册");
        return "/register";
    }
}
